//
//  CZComposeController.swift
//  GZWeibo007
//
//  Created by danli on 15/12/26.
//  Copyright © 2015年 Apple. All rights reserved.
//

import UIKit
import SVProgressHUD

enum CZComposeControllerType:Int
{
    case composeControllerTypeComment = 0
    case composeControllerTypeReport = 1
}

enum CZComposeNotification:String
{
    case commentNotification = "didCommentNotification"
    case reportNotification = "didReportNotification"
}


class CZComposeController: UIViewController {
    
    // MARK: 属性
    let status: CZStatus?
    //控制器类型
    let vcType: CZComposeControllerType
    

    var text:String?
    

    // MARK: 构造方法
    init(status:CZStatus?, vcType:CZComposeControllerType)
    {
        self.status = status
        self.vcType = vcType
        super.init(nibName: nil, bundle: nil)
    }

    required init?(coder aDecoder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }
    
    override func viewDidLoad() {
        super.viewDidLoad()
        prepareUI()
    }
    
    func prepareUI()
    {
        //设置导航栏
        navigationItem.leftBarButtonItem = UIBarButtonItem(title: "取消", style: UIBarButtonItemStyle.Plain, target: self, action: "closeVC")
        navigationItem.rightBarButtonItem = UIBarButtonItem(title: "发送", style: UIBarButtonItemStyle.Plain, target: self, action: "send")
        
        // 设置titleView
        navigationItem.titleView = titleLabel
        // 获取用户名
        let userName = (CZUserAccount.loadUserAccount()?.screen_name)!

        //根据不同的控制器设置 attriStr
        var titleText :String
        switch vcType
        {
        case CZComposeControllerType.composeControllerTypeComment:
            titleText = "发表评论: \n\(userName)"
        case CZComposeControllerType.composeControllerTypeReport:
            titleText = "转发微博: \n\(userName)"
        }
        let range = (titleText as NSString).rangeOfString(userName)
        let attributeStr = NSMutableAttributedString(string: titleText)

        attributeStr.addAttribute(NSFontAttributeName, value:UIFont.systemFontOfSize(13) , range: range)
        attributeStr.addAttribute(NSForegroundColorAttributeName, value: UIColor.darkGrayColor(), range: range)
        titleLabel.attributedText = attributeStr
        titleLabel.sizeToFit()
        
        // 添加textLabel
        view.addSubview(textView)
        textView.ff_Fill(view)
    }
    
    /// 发送微博或者评论微博
    func send()
    {
        let text = textView.text
        print("text = \(text)")
        if text?.characters.count > 140 || text?.characters.count == 0
        {
            SVProgressHUD.showErrorWithStatus("微博内容要在1~140字符之间")
            return
        }
        
        if vcType == CZComposeControllerType.composeControllerTypeComment
        {
            //评论
            print("发表评论")
            SVProgressHUD.showWithStatus("正在评论微博...", maskType: SVProgressHUDMaskType.Black)
            
            guard let id = status?.id else
            {
                print("评论微博没有id")
                return
            }
            
            // 发送评论内容到服务器
            CZNetworkTool.sharedInstance.sendComment(text!, id: id, finished: { (error) -> () in
                if error != nil {
                    print("error:\(error)")
                    SVProgressHUD.showErrorWithStatus("发送评论失败...", maskType: SVProgressHUDMaskType.Black)
                    return
                }
                
        NSNotificationCenter.defaultCenter().postNotificationName(CZComposeNotification.commentNotification.rawValue, object: self)
            self.closeVC()
            })

        }
        else
        {
            //转发微博
            print("转发微博")
            SVProgressHUD.showWithStatus("正在转发微博...", maskType: SVProgressHUDMaskType.Black)
            
            guard let _ = status?.id else
            {
                print("没有转发微博的id")
                return
            }

            // 发送转发微博到服务器
            CZNetworkTool.sharedInstance.repostStatus(text!, id: status!.id, finished: { (error) -> () in

                if error != nil {
                    print("error:\(error)")
                    SVProgressHUD.showErrorWithStatus("转发失败", maskType: SVProgressHUDMaskType.Black)
                    return
                }

            NSNotificationCenter.defaultCenter().postNotificationName(CZComposeNotification.reportNotification.rawValue, object: self)

                self.closeVC()
            })
        }
    }
    
    func closeVC()
    {

        self.textView.resignFirstResponder()
        SVProgressHUD.dismiss()
        dismissViewControllerAnimated(true, completion: nil)
    }
    
    // MARK: 懒加载
    lazy var titleLabel:UILabel = {
        let label = UILabel()
        label.textColor = UIColor.blackColor()
        label.textAlignment = NSTextAlignment.Center
        label.numberOfLines = 0
        return label
    }()
    
    lazy var textView:UITextView = {
       let textView = UITextView()
        textView.backgroundColor = UIColor.lightGrayColor()
        textView.font = UIFont.systemFontOfSize(18)
        return textView
    }()
}
